<?php

namespace Gnway\Ticket\Rest\Mapping;

use Gnway\Ticket\Rest\Exceptions\ClassNoDefException;
use Gnway\Ticket\Rest\Exceptions\PropertyInjectErrorException;

class PropertyMapping
{

  /**
   * @var array
   */
  protected array $propertiesMapping = [];

  /**
   * Inject mapping
   *
   * @param string $class
   * @param string $property
   * @param string $value
   * @return void
   */
  public function bind(string $class, string $property, string $value): void
  {
    if ( ! class_exists($class)) {
      throw new ClassNoDefException('PropertyMapping class ' . $class . ' not defined');
    }
    if ($property === '') {
      throw new PropertyInjectErrorException('PropertyMapping class ' . $class . 'property name cannot be empty');
    }
    if ( ! property_exists($class, $property)) {
      throw new PropertyInjectErrorException('PropertyMapping property "' . $property . '" not exists');
    }
    if ( ! class_exists($value)) {
      throw new PropertyInjectErrorException('PropertyMapping inject value invalid');
    }

    $this->propertiesMapping[$class][$property] = $value;
  }

  /**
   * Copy mapping
   *
   * @param PropertyMapping|null $from
   */
  public function copy(?PropertyMapping $from)
  {
    if ($from !== NULL) {
      $this->propertiesMapping = $from->getPropertiesMapping();
    }
  }

  /**
   * Get Mappings
   *
   * @return array
   */
  public function getPropertiesMapping(): array
  {
    return $this->propertiesMapping;
  }
}